package ua.pp.pycb6a.seposition.helper;

import static ua.pp.pycb6a.seposition.helper.LoggerHelper.getLogger;
import static ua.pp.pycb6a.seposition.helper.PropertyHelper.getWebAddress;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;

public final class WriterHelper {

    private static PrintWriter writer;

    private WriterHelper() {

    }

    public static PrintWriter getWriter() {

        return writer;
    }

    public static void setWriter(String filename) {

        try {
            File file = new File(filename);
            file.createNewFile();
            writer = new PrintWriter(file);
        } catch (IOException e) {

            getWriter().println("RESULT FILE NOT FOUND");
        }
    }

    public static void closeWriter() {

        getWriter().close();
    }

    public static void writeTitle(String searhEngine) {

        getWriter().println();
        writeLine();
        getWriter().println("SEARCH ENGINE: " + searhEngine);
        writeLine();
        getWriter().println("WEB ADDRESS: " + getWebAddress());
        writeLine();
    }

    public static void writeTime() {

        writeLine();
        getWriter().println(Calendar.getInstance().getTime());
        writeLine();

    }

    public static void writeLine() {

        getWriter().println(
                "------------------------------------------------------------");
    }

    public static void writeAnalysisResult(int position, String uri) {

        if (position == 0) {
            getWriter().println(" NO DATA\n");
            getLogger().info("NO DATA\n");

        } else {
            getWriter().println(String.format(" POSITION: %d", position));
            getLogger().info(String.format("POSITION: %d", position));

            getWriter().println(String.format("URI: %s", uri));
            getWriter().println();
            getLogger().info(String.format("URI: %s", uri));
        }
    }
}
